<%@ include file="/WEB-INF/views/taglib.jsp" %>
<h3>Delivery Order Form</h3>	
	<form:form id="form" method="post" modelAttribute="deliveryOrder"  cssClass="simpleForm deliveryOrderForm" >
		<form:hidden path="deliveryOrderId" />
		 <div class="action">
	  	<c:choose>
	  		<c:when test="${deliveryOrder.deliveryOrderId==null}">
	  			<button type="button" class="button positive" onclick="formSubmit()">
	  			Confirm
	  			</button>
	  			<a href="<c:url value="/deliveryOrder/create" />" class="button positive">Cancel</a>
	  			<button type="button" class="button positive" onclick="location.href='<c:url value="/deliveryOrder/search" />'">Back</button>
	  		</c:when>
	  		<c:when test="${deliveryOrder.deliveryOrderId!=null}">
	  			<a href="<c:url value="/deliveryOrder/print/${deliveryOrder.deliveryOrderId}" />" target="_blank" class="button">Print</a>
  				<button type="button" class="button positive" onclick="location.href='<c:url value="/deliveryOrder/search" />'">Back</button>
	  		</c:when>
	  	</c:choose>
	 	</div>	
		<div class="clear" style="height:1px">&nbsp;</div>
		<jsp:include page="/WEB-INF/views/message.jsp"></jsp:include>
	  	<fieldset>
	  		<legend>Delivery Order Info</legend>
	  		<div class="span-11">
				  		<aims:column cssErrorClass="error" path="deliveryOrderCode" >
				  		<form:label path="deliveryOrderCode">
				  			DO No
				 		</form:label>
				  		<form:input path="deliveryOrderCode" /> 
				  		<form:errors path="deliveryOrderCode"  cssClass="errorSimple" /> 
						</aims:column>
			
			
						<c:choose>
				  			<c:when test="${deliveryOrder.deliveryOrderId==null}">
						<aims:column cssErrorClass="error" path="purchaseOrder" >
				  		<form:label path="purchaseOrder">
				  			PO No 
				 		</form:label>  	
				  		<form:select id="purchaseOrderId" path="purchaseOrder.purchaseOrderId" class="purchaseOrderId">
				 			<form:option value=""></form:option>
				 			<form:options items="${purchaseOrderList}" itemValue="purchaseOrderId" itemLabel="purchaseOrderCode" />				 			
				 		</form:select>
				  		<form:errors path="purchaseOrder"  cssClass="errorSimple" /> 
						</aims:column>	
						</c:when>
						
						<c:when test="${deliveryOrder.deliveryOrderId!=null}">
						<p class="span-11">
						<label>
				  			PO No 
				 		</label>
				 		<c:out value="${deliveryOrder.purchaseOrder.purchaseOrderCode}" />
				 		</p> 
						</c:when>
						</c:choose>
						
						
						<p class="span-11">
						<label>
				  			Vendor No		  			
				 		</label>
				 		<c:out value="${deliveryOrder.purchaseOrder.vendor.vendorId}" />
						</p>
						
					<p class="span-11">
						<label>
				  			Vendor Name
				 		</label>
						<c:out value="${deliveryOrder.purchaseOrder.vendor.vendorName}" />
						</p>	
						
					<p class="span-11">
						<label>Address</label>
						<c:out value="${deliveryOrder.purchaseOrder.vendor.contactInfo.contactAddress}"/>
						</p>
						
						
						<p class="span-11">
						<label>Contact No</label>
						<c:out value="${deliveryOrder.purchaseOrder.vendor.contactInfo.contactNo}"/>
						</p>
						
						
			</div>	
				
			<div class="span-11">
				
						<aims:column cssErrorClass="error" path="deliveryOrderDate" >
				  		<form:label path="deliveryOrderDate">
				  			DO Date  * 
				 		</form:label>
				  		<form:input path="deliveryOrderDate" id="doDate" onkeypress="return restrictCharacters(this, event, validChars);"/> 
				  		<input id="doDate_button" type="button" class="calendarBtn" />
				  		<form:errors path="deliveryOrderDate"  cssClass="errorSimple" /> 
						</aims:column>
						
						
						<p class="span-11">
						<label> Asset Type</label>
						<c:out value="${deliveryOrder.purchaseOrder.assetType}"/>
	  					</p>
	  					
	  					
	  					<p class="span-11">
						<label> Receiving Location</label>
						<c:out value="${deliveryOrder.purchaseOrder.location.locationName}"/>
	  					</p>
	  						  				
	  					
	  					<p class="span-11">
						<label> PO Date</label>
						<c:out value="${deliveryOrder.purchaseOrder.purchaseOrderDateToDisplay}"/>
	  					</p>
	  					
	  					<p class="span-11">
						<label>Access Dept</label>
						<c:choose>
	  					<c:when test="${deliveryOrder.purchaseOrder.accessDepartment.departmentName!=null}">
	  					<c:out value="${deliveryOrder.purchaseOrder.accessDepartment.departmentName}"/>
	  					</c:when>
	  					<c:otherwise>
	  					-
	  					</c:otherwise>
	  					</c:choose>
	  					</p>
	  					
	  					<p class="span-11">
						<label> Company Code</label>
						<c:out value="${deliveryOrder.purchaseOrder.companyCode}"/>
	  					</p>  		
	  					
	  					
			</div>
			
			</fieldset>
			
			<fieldset>
			
			<legend>Asset Info</legend>		
	  	
	  		<table id="assetTable" class="gridSearch">
	  			<thead>
	  				<tr>
	  				    <th width="4%">S/N</th>
	  				    <th width="40%">Asset Model</th>
	  					<th width="14%">Order Qty</th>
	  					<th width="14%">Stock-In/Received Qty</th>
	  					<th width="14%">Yet To Receive</th>
	  					<th width="14%">Currently Receiving</th>
	  				</tr>
	  			</thead>
	  			
	  			<tbody>
	  			<c:choose>
	  			<c:when test="${deliveryOrder.deliveryOrderId==null}">
	  			<c:forEach items="${deliveryOrder.purchaseOrder.purchaseOrderDetails}" var="item" varStatus="status">
	  				<tr <c:if test="${(status.count%2)==0 }"> class="alt"</c:if>>
	  						<input name="deliveryOrderComponents[${status.index}].assetModel.assetModelId" type="hidden" value="${item.assetModel.assetModelId}"/>
	  						<input name="deliveryOrderComponents[${status.index}].assetModel.assetModelCode" type="hidden" value="${item.assetModel.assetModelCode}"/>
							<input name="deliveryOrderComponents[${status.index}].quantityOrder" type="hidden" value="${item.quantityOrder}"/>
							<input name="deliveryOrderComponents[${status.index}].quantityReceived" type="hidden" value="${item.quantityReceived}"/>
							
							<td align="center" >
	  						<c:out value="${status.index+1}"/>
	  						</td>
	  								  						
		  					<td>
		  					<c:out value="${item.assetModel.assetModelCode}-${item.assetModel.assetModelName}" />
		  					</td>
		  					
		  					<td>
		  					<c:out value="${item.quantityOrder}" />
		  					</td>
		  					
		  					<c:set var="quantityReceived" value="${item.quantityReceived}"/>
		  					<c:set var="quantityOrder" value="${item.quantityOrder}"/>
		  					<c:set var="balanceQuantity" value="${quantityOrder-quantityReceived}"/>
		  					
		  					<td>
		  					<c:out value="${item.quantityReceived}" />
		  					</td>
		  					
		  					<td>
		  					<input name="balanceQuantity" class = "balanceQuantity" type="hidden" value="${balanceQuantity}"/>
							<c:out value="${balanceQuantity}" />
		  					</td>
		  				
		  					<td>
		  					<input name="deliveryOrderComponents[${status.index}].quantityReceiving" type="text" 
		  					size="8" value="${qtyReceiving[status.index]}"
	  						class="quantityReceiving" />
		  					</td>
		  									 
	  			</tr>
	  					
	  			</c:forEach>
	  			</c:when>
	  			
	  			<c:when test="${deliveryOrder.deliveryOrderId!=null}">
	  			<c:forEach items="${deliveryOrder.deliveryOrderComponents}" var="item" varStatus="status">
	  				<tr <c:if test="${(status.count%2)==0 }"> class="alt"</c:if>>
	  						<td align="center">
	  						<c:out value="${status.index+1}"/>
	  						</td>
	  								  						
		  					<td>
		  					<c:out value="${item.assetModel.assetModelCode}-${item.assetModel.assetModelName}" />
		  					</td>
		  					
		  					<c:set var="quantityReceived" value="${item.quantityReceived}"/>
		  					<c:set var="quantityOrder" value="${item.quantityOrder}"/>
		  					<c:set var="balanceQuantity" value="${quantityOrder-quantityReceived}"/>
		  					
		  					<td>
		  					<c:out value="${item.quantityOrder}" />
		  					</td>
		  					
		  					<td>
		  					<c:out value="${item.quantityReceived}" />
		  					</td>
		  					
		  					<td>
		  					<c:out value="${balanceQuantity}" />
		  					</td>
		  				
		  					<td>
		  					<c:out value="${item.quantityReceiving}" />
		  					</td>
		  									 
	  					</tr>
	  					
	  			</c:forEach>
	  			
	  			</c:when>
	  			</c:choose>
	  			</tbody>
	  		
	  		</table>
			</fieldset>
			
			
			
			
      <div class="action">
	  	<c:choose>
	  		<c:when test="${deliveryOrder.deliveryOrderId==null}">
	  			<button type="button" class="button positive" onclick="formSubmit()">
	  			Confirm
	  			</button>
	  			<a href="<c:url value="/deliveryOrder/create" />" class="button positive">Cancel</a>
	  			<button type="button" class="button positive" onclick="location.href='<c:url value="/deliveryOrder/search" />'">Back</button>
	  		</c:when>
	  		<c:when test="${deliveryOrder.deliveryOrderId!=null}">
	  			<a href="<c:url value="/deliveryOrder/print/${deliveryOrder.deliveryOrderId}" />" target="_blank" class="button">Print</a>
  				<button type="button" class="button positive" onclick="location.href='<c:url value="/deliveryOrder/search" />'">Back</button>
	  		</c:when>
	  	</c:choose>
	 </div>	
	
	
	</form:form>
	
	<script type="text/javascript" src="<spring:url value="/resources/js/select-option-disabled-emulation.js" />"></script>
	<script type="text/javascript" src="<spring:url value="/resources/js/restrictCharactersForDateInput.js" />"></script>
	<script type="text/javascript">
	
				
	var validChars = /[1234567890-]/g;
	
		$(document).ready(function() {
			
			$(".quantityReceiving").numeric(false, function() { this.value = ""; this.focus(); });				
			cal = Calendar.setup({
		          onSelect: function(cal) { cal.hide() },
		          showTime: false
		      });
			rebuiltCalendar();
			$("#purchaseOrderId option[value='']").attr("disabled",true);
		});
		
		
		
		$('.purchaseOrderId').change(function(){
				var purchaseOrderId =  $('#purchaseOrderId').val();
				var url = "<c:url value="/deliveryOrder/creates"/>";
				var form = $('#form');				
				redirect(form,url);
				
		});
		
		function redirect(form,url){
			form.attr('action',url );
			form.submit();
		}
		
		function formSubmit(){
			if(validateForm()){
				var url = "<c:url value="/deliveryOrder/create"/>";
				var form = $('#form');				
				redirect(form,url);
			}
		}
		
		function validateForm(){
			var table = $(' #assetTable tbody tr');
		    var res = true;
			var purchaseOrderId =  $('#purchaseOrderId').val();
			if(purchaseOrderId == '')
				{	
				    alert(" Pls do select Purchase Order to start processing Delivery Order");
					res = false;
					return res;
				}
			
			var quantityReceiving;
			var balanceQuantity;
			table.each(
					function (index) {						
						quantityReceiving = $('.quantityReceiving',this).val();
						balanceQuantity = $('.balanceQuantity',this).val();
						
						if(quantityReceiving=='' ){
							alert(" Pls do enter the Currently Receiving Qty in row "+(index+1));
							res = false;
							return false;
						}
						
						else if(parseInt(balanceQuantity) < parseInt(quantityReceiving)){
							alert(" Currently Receiving Qty should not be greater than Yet to Receive Quantity in row "+(index+1));
							res = false;
							return false;
						}
					}
					)					
					;
			
			return res;
			
		}
		
		function rebuiltCalendar()
		{
			cal.manageFields("doDate_button", "doDate", globalJsCalDateFormat);
			  
		}
			
	</script>
